package br.fontes;

import java.util.ArrayList;
import java.util.List;


public class FuncionarioDao extends conexao {
	
	
	public void create(Funcionario f) throws Exception{		
		open();
			stmt = con.prepareStatement("insert into pessoa (nome, salario) values(?,?)");
			stmt.setString(1, f.getNome());
			stmt.setDouble(2, f.getSalario());
			stmt.execute();
			stmt.close();
		close();		
	}

	public void delete(int cod) throws Exception{		
		open();
			stmt = con.prepareStatement("delete from pessoa where id = ?");
			stmt.setInt(1, cod);
			stmt.execute();
			stmt.close();
		close();		
	}

	public void update(Funcionario f) throws Exception{		
		open();
			stmt = con.prepareStatement("update pessoa set nome = ? , salario = ? where id = ?");
			stmt.setString(1, f.getNome());
			stmt.setDouble(2, f.getSalario());
			stmt.setInt(3, f.getId());
			stmt.execute();
			stmt.close();
		close();		
	}
	
	public List findAll() throws Exception{		
		open();
			stmt = con.prepareStatement("select * from pessoa");
			rs = stmt.executeQuery();
				List lista = new ArrayList();
			while(rs.next()){
				Funcionario funcionario = new Funcionario();
				funcionario.setId(rs.getInt(1));
				funcionario.setNome(rs.getString(2));
				funcionario.setSalario(rs.getDouble(3));
				lista.add(funcionario);
                                System.out.println(lista);
			}
			stmt.close();
		close();
		return lista;
	}
        
        public Funcionario findByCode(int id) throws Exception{		
		open();
			stmt = con.prepareStatement("select * from pessoa where id = ?");
			stmt.setInt(1, id);	
			rs = stmt.executeQuery();
				Funcionario funcionario	= null;			
			if(rs.next()){
				funcionario = new Funcionario();
				funcionario.setId(rs.getInt(1));
				funcionario.setNome(rs.getString(2));
				funcionario.setSalario(rs.getDouble(3));
				
			}
			stmt.close();
		close();
		return funcionario;
	}	
}